"""
datetime模块的使用
    提示: datetime.time + datetime.date = datetime.datetime
"""
from utils.test_tools import test_decorator
import datetime
import time


@test_decorator('datetime.date')
def test_date():
    """
    1、date模块
    公历的一天 属性:year month day
    :return: None
    """
    dater = datetime.date

    # 获取当天年月日
    today = dater.today()
    # today = date.fromtimestamp(time.time())
    print('datetime > date > today(): ', today, ' type: ', type(today))
    print('datetime > date > fromtimestamp(time.time()):', dater.fromtimestamp(time.time()),
          ' type: ', type(dater.fromtimestamp(time.time())))

    # today 的属性年月日
    print(today.year, today.month, today.day)
    print(str(today))

    # 年月日的差值
    oneday = dater(2008, 8, 8)
    print((today - oneday).days)

    # replace
    print(today.replace(year=2018))


@test_decorator('datetime.time')
def test_time():
    """
    2、time模块
    一天的时间
    属性: hour, minute, second, microsecond, tzinfo
    :return: None
    """
    timer = datetime.time

    # 当前时间
    now = timer(13, 23, 45)
    print('datetime > time: ', now, ' type: ', type(now))

    # 属性
    print('now.hour: %s, now.minute: %s, now.second: %s, now.microsecond %s'
          % (now.hour, now.minute, now.second, now.microsecond))

    # 精确到hours minutes seconds
    # print(now.isoformat(timespec='hours'))
    print(now.isoformat(timespec='minutes'))
    print(now.isoformat(timespec='seconds'))

    # replace
    print(now.replace(hour=12))


@test_decorator('datetime.datetime')
def test_datetime():
    """
    3、datetime模块

    :return: None
    """
    datetimer = datetime.datetime

    # 当前时间
    now = datetimer.now()
    print(now)

    # 属性
    print('now.year: %s, now.month: %s, now.day: %s, now.hour: %s, now.minute: %s, now.second: %s'
          % (now.year, now.month, now.day, now.hour, now.minute, now.second))

    # 当前日期
    today = datetimer.today()
    print(today)

    # 格式化输出
    print(now.isoformat())
    # 周
    print(today.isoweekday())
    print(today.isocalendar())


@test_decorator('datetime.timedelta')
def test_timedelta():
    """
    4、timedelta模块
    timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
    :return: None
    """
    timedeltar = datetime.timedelta

    oneyear = timedeltar(days=365)
    another_year = timedeltar(weeks=40, days=84, hours=23, minutes=50, seconds=600)

    print('one year total seconds: ', oneyear.total_seconds(), another_year.total_seconds())
    print(oneyear == another_year)

    three_years = oneyear * 3
    nine_years = another_year * 10 - oneyear

    print(abs(three_years - nine_years) == three_years * 2)

# 5、timezone模块


if __name__ == '__main__':
    test_date()

    test_time()

    test_datetime()

    test_timedelta()

